from PySide6.QtCore import QIODevice, QObject, QTimer, Slot, Signal, Property, QEnum, QThread
from PySide6.QtQml import QmlElement, QmlSingleton
from PySide6.QtSerialPort import QSerialPortInfo, QSerialPort
from PySide6.QtGui import QGuiApplication
import logging
import re
from enum import Enum
from .SshWorker import SshWorker

    
# 定义 QML 模块名称和版本
QML_IMPORT_NAME = "ThreadedSshClient"
QML_IMPORT_MAJOR_VERSION = 1

@QmlElement
class ThreadedSshClient(QObject):
    _connectedStateChanged = Signal()
    
    # Signals to sshWorker
    _connectToHost = Signal(str, int, str, str)
    _disconnectFromHost = Signal()
    _execCommand = Signal(str)
    _update_install = Signal(str)
    
    _getbacked_env = Signal()
    _setbacked_env = Signal(str)
    
    # Signals from sshWorker
    updateInstallFinished = Signal(bool, str)
    
    getBackedEnvTypeFinished = Signal(bool, str)
    setBackedEnvTypeFinished = Signal(bool, str)
    
    def __init__(self, parent=None):
        super().__init__(parent)
        self._sshWorker = SshWorker()
        self._workThread = QThread()
        self.app = QGuiApplication.instance()

        # 连接信号
        self._setup_signal_connections()
        
        # 保证线程在应用程序退出时正确退出
        self.app.aboutToQuit.connect(self._workThread.quit)
        
        self._sshWorker.moveToThread(self._workThread)
        self._workThread.start()
    
    @Property(bool, notify=_connectedStateChanged)
    def connectedState(self):
        return self._sshWorker.isConnected()
    
    def _setup_signal_connections(self):
        self._sshWorker.connectedStateChanged.connect(self._connectedStateChanged)
        self._connectToHost.connect(self._sshWorker.connectToHost)
        self._disconnectFromHost.connect(self._sshWorker.disconnectFromHost)
        self._execCommand.connect(self._sshWorker.execCommand)
        self._update_install.connect(self._sshWorker.update_install)
        self._getbacked_env.connect(self._sshWorker.getBackedEnv)
        self._setbacked_env.connect(self._sshWorker.setBackedEnv)
        
        self._sshWorker.updateInstallFinished.connect(self.updateInstallFinished)
        self._sshWorker.getBackedEnvTypeFinished.connect(self.getBackedEnvTypeFinished)
        self._sshWorker.setBackedEnvTypeFinished.connect(self.setBackedEnvTypeFinished)
    
    # 连接到主机
    @Slot(str, int, str, str)
    def connectToHost(self, host, port, username, password):
        self._connectToHost.emit(host, port, username, password)
    
    # 断开连接
    @Slot()
    def disconnectFromHost(self):
        self._disconnectFromHost.emit()
    
    # 执行命令
    @Slot(str)
    def execCommand(self, command):
        self._execCommand.emit(command)
        
    # 更新软件
    @Slot(str)
    def updateInstall(self, file_path: str):
        self._update_install.emit(file_path)

    # 获取后台环境信息，demo,dev,pruduction
    @Slot() 
    def getBackedEnvType(self): 
        self._getbacked_env.emit() 

    # 设置后台环境信息，demo,dev,pruduction
    @Slot(str)
    def setBackedEnvType(self, env: str):
        self._setbacked_env.emit(env)